package com.kangfu.api.service.impl;


import cn.hutool.core.util.StrUtil;
import com.kangfu.api.service.IUserService;
import com.kangfu.common.constant.WechatConstant;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.common.utils.RedisUtils;
import com.kangfu.db.domain.entity.User;
import com.kangfu.db.mapper.UserMapper;
import com.kangfu.db.model.request.UserInfoRequest;
import com.kangfu.wechat.api.UserApi;
import com.kangfu.wechat.bean.result.Phone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-12
 */
@Service
public class UserServiceImpl implements IUserService
{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    public RedisUtils redisUtils;

    /**
     * 更新用户手机号
     * @param code
     * @param userId
     * @return
     */
    @Override
    public int updateUserPhone(String code, int userId) {
        User user = new User();
        user.setId(userId);
        Phone phone = UserApi.getUserPhone(WechatConstant.APPID, WechatConstant.SECRET, code);
        if (phone == null || 0 != phone.getErrcode()) {
            throw new BusinessException("获取用户手机号失败");
        }
        String phoneNum = phone.getPhone_info().getPhoneNumber();
        user.setPhone(phoneNum);
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateUser(user);
    }

    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public User selectUserById(int id)
    {
        return userMapper.selectUserById(id);
    }

    /**
     * 查询用户
     * @param openid
     * @return
     */
    public User selectUserByOpenid(String openid) {
        return userMapper.selectUserByOpenid(openid);
    }

    /**
     * 新增用户
     * 
     * @param user 用户
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        user.setCreateTime(LocalDateTime.now());
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户
     * 
     * @param userId 用户
     * @param UserInfoRequest 用户
     * @return 结果
     */
    @Override
    public int updateUser(int userId, UserInfoRequest userInfoRequest)
    {
        //先查询用户
        User user = userMapper.selectUserById(userId);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        //校验验证码
        if (!StrUtil.isBlank(userInfoRequest.getCode()) && !StrUtil.isBlank(userInfoRequest.getNewPhone())) {
            String code = (String) redisUtils.get("change_phone:"+userInfoRequest.getNewPhone());
            if (code == null) {
                throw new BusinessException("验证码已过期");
            }
            if (!userInfoRequest.getCode().equals(code)) {
                throw new BusinessException("验证码错误");
            }
            user.setPhone(userInfoRequest.getNewPhone());
        }
        user.setAvatar(userInfoRequest.getAvatar());
        user.setAddress(userInfoRequest.getAddress());
        user.setArea(userInfoRequest.getArea());
        user.setAreaCode(userInfoRequest.getAreaCode());
        user.setBirthday(userInfoRequest.getBirthday());
        user.setNickname(userInfoRequest.getNickname());
        user.setSex(userInfoRequest.getSex());
        user.setUpdateTime(LocalDateTime.now());
        System.out.println(user.getBirthday());
        return userMapper.updateUser(user);
    }


}
